package com.jbh.fourclibrary.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;
import com.jbh.fourclibrary.R;
import com.jbh.fourclibrary.adapter.BaseRecyclerViewAdapter;
import com.jbh.fourclibrary.util.ConstantUtil;
import com.jbh.fourclibrary.util.Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ShapeFragment extends Fragment implements View.OnClickListener{

    private View view;
    private RecyclerView shapeRecyclerView;
    private ImageView img_show_large;

    public ShapeFragment() {
    }

    public static ShapeFragment newInstance() {
        ShapeFragment fragment = new ShapeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_shape, container, false);
        initView();
        initData();
        initShapeList();
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
    }


    int defaultTabHeight ;
    int defaultTabWidth ;

    private void initView() {
        defaultTabHeight = Util.dpToPx(getResources(),80);
        defaultTabWidth =  Util.dpToPx(getResources(),90);

        view.findViewById(R.id.onLeft).setOnClickListener(this);
        view.findViewById(R.id.onRight).setOnClickListener(this);
        img_show_large=view.findViewById(R.id.img_show_large);
        ViewGroup.LayoutParams param = img_show_large.getLayoutParams();
        param.height = Util.getHeight(getActivity()) - Util.getStatusBarHeight(getActivity()) -
                defaultTabHeight - Util.dpToPx(getActivity(),120);

        shapeRecyclerView=view.findViewById(R.id.shapeRecyclerView);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        shapeRecyclerView.setLayoutManager(linearLayoutManager);
    }

    private BaseRecyclerViewAdapter adapter;
    private void initShapeList() {
        adapter=new BaseRecyclerViewAdapter(getActivity(),R.layout.item_shape_layout,list.size()){

            @Override
            public void content(RecyclerView.ViewHolder holder, final int position) {
                super.content(holder, position);

                ViewGroup.LayoutParams param = holder.itemView.getLayoutParams();
                param.height = defaultTabHeight;
                param.width = defaultTabWidth;


                final ShapeBean shape = list.get(position);
                ImageView img_show=holder.itemView.findViewById(R.id.img_show);
                img_show.setImageDrawable(Util.GetTintDrawable(getActivity(),shape.getShape(),R.color.white));

                TextView tvName=holder.itemView.findViewById(R.id.tvName);
                tvName.setTextColor(Color.WHITE);
                tvName.setText(shape.getName());

                if (shape.isCheck()){
                    holder.itemView.setBackgroundResource(R.mipmap.icon_shape_press);
                }else {
                    holder.itemView.setBackgroundResource(0);
                }

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (ShapeBean l:list){
                            l.setCheck(false);
                        }
                        selectPosition = position ;
                        img_show_large.setImageResource(ConstantUtil.getShapeImage0().get(shape.getName()));
                        shape.setCheck(true);
                        notifyDataSetChanged();
                    }
                });
            }
        };
        shapeRecyclerView.setAdapter(adapter);
    }


    /**
     *
     */
    public void onLeft(){
        if (selectPosition>0){
            selectPosition -- ;
            for (ShapeBean l:list){
                l.setCheck(false);
            }
            img_show_large.setImageResource(ConstantUtil.getShapeImage0().get(list.get(selectPosition).getName()));
            list.get(selectPosition).setCheck(true);
            if (adapter!=null)adapter.notifyDataSetChanged();
            shapeRecyclerView.scrollToPosition(selectPosition);
        }
    }


    /**
     *
     */
    public void onRight(){
        if (selectPosition<(list.size()-1)){
            selectPosition ++ ;
            for (ShapeBean l:list){
                l.setCheck(false);
            }
            img_show_large.setImageResource(ConstantUtil.getShapeImage0().get(list.get(selectPosition).getName()));
            list.get(selectPosition).setCheck(true);
            if (adapter!=null)adapter.notifyDataSetChanged();
            shapeRecyclerView.scrollToPosition(selectPosition);
        }
    }


    private int selectPosition=0;
    private List<ShapeBean> list=new ArrayList<>();
    public void initData(){
        list.clear();
        HashMap<String, Integer> map = ConstantUtil.getShapeImage();
        Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String, Integer> entry = it.next();
            ShapeBean shapeBean=new ShapeBean();
            shapeBean.setCheck(false);
            shapeBean.setName(entry.getKey());
            shapeBean.setShape(entry.getValue());
            list.add(shapeBean);
        }
        list.get(0).setCheck(true);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.onLeft) {
            onLeft();
        } else if (id == R.id.onRight) {
            onRight();
        }
    }


    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        Log.v("=======enter======","enter="+enter);
        return super.onCreateAnimation(transit, enter, nextAnim);
    }


    class ShapeBean{
        String name;
        int shape;
        boolean isCheck;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getShape() {
            return shape;
        }

        public void setShape(int shape) {
            this.shape = shape;
        }

        public boolean isCheck() {
            return isCheck;
        }

        public void setCheck(boolean check) {
            isCheck = check;
        }
    }
}
